package org.dao;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.model.Adcg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.util.SomeTool;

@Repository(value = "dao")
public class Dao {
	@Autowired
	private SessionFactory sessionFactory;
	
	public Adcg get(String model,Integer start,Integer count){
		Session session=sessionFactory.openSession();
		ArrayList<String> fields=SomeTool.modelFields(model);
		StringBuilder sb=new StringBuilder();
		for(String field:fields){
			sb.append(field+",");
		}
		String hql="select " + sb.substring(0, sb.length()-1) + " from "+SomeTool.firstUp(model) + " order by "+SomeTool.modelFields(model).get(0) + " desc";
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(count);
		@SuppressWarnings("unchecked")
		ArrayList<String[]> values = (ArrayList<String[]>) query.list();
		Adcg acgd=new Adcg();
		acgd.setModel(model);
		acgd.setFields(fields);
		acgd.setValues(values);
		query=session.createQuery("select count(*) from " + SomeTool.firstUp(model));
		acgd.setCount((Long)query.uniqueResult());;
		session.close();
		return acgd;
	}
	public boolean delete(Integer id,String model){
		Session session=sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(session.get(Class.forName("org.model."+SomeTool.firstUp(model)), id));
			tx.commit();
		} catch (Exception e) {
			return false;
		}finally{
			session.close();
		}
		return true;
	}
	public boolean change(Integer id,String model,ArrayList<String> fields,ArrayList<String> values){
		Session session=sessionFactory.openSession();
		StringBuilder sb=new StringBuilder();
		int i=0;
		for(String field:fields){
			if(values.get(i)==null||"".equals(values.get(i))){
				sb.append(field+"=null,");
				i++;
				continue;
			}
			if("true".equals(values.get(i))||"false".equals(values.get(i))){
				sb.append(field+"="+values.get(i)+",");
				i++;
				continue;
			}
			if(field.indexOf(".")!=-1){
				sb.append(field.substring(0,field.indexOf("."))+"="+"'"+values.get(i++)+"',");
			}else{
				sb.append(field+"="+"'"+values.get(i++)+"',");
			}
		}
		String hql="update " + SomeTool.firstUp(model) + " set " + 
				sb.substring(0, sb.length()-1) + " where " + SomeTool.modelFields(model).get(0) + "=" + id;
		System.out.println(hql);
		Query query = session.createQuery(hql);
		int noErr=query.executeUpdate();
		session.close();
		return noErr==1;
	}
	public boolean add(String model){
		Session session=sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		try {
			Object obj=Class.forName("org.model."+SomeTool.firstUp(model)).newInstance();
			session.save(obj);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}finally{
			session.close();
		}
		return true;
	}
}
